import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_BASE_API } = env;
  return {
    base: "/",
    plugins: [
      vue(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), "src/assets/svg")],
        // 指定symbolId格式
        // symbolId: "icon-[dir]-[name]",
        symbolId: "icon-[name]",
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src"),
      },
    },
    server: {
      host: "0.0.0.0",
      open: true,
      port: 89,
      proxy: {
        // 把key的路径代理到target位置
        [VITE_BASE_API]: {
          // 需要代理的路径   例如 '/api'
          target: "http://localhost:3000", // 代理到 目标路径
          changeOrigin: true,
          secure: false, // https校验
          rewrite: (path) => path.replace(new RegExp("^" + VITE_BASE_API), ""),
        },
      },
    },
  };
});
